<?php

namespace app\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\Session;


class Index extends BaseController
{
//    public function index()
//    {
//        return '<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px;} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:) </h1><p> ThinkPHP V' . \think\facade\App::version() . '<br/><span style="font-size:30px;">14载初心不改 - 你值得信赖的PHP框架</span></p><span style="font-size:25px;">[ V6.0 版本由 <a href="https://www.yisu.com/" target="yisu">亿速云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=64890268" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ee9b1aa918103c4fc"></think>';
//    }
//
//    public function hello($name = 'ThinkPHP6')
//    {
//        return 'hello,' . $name;
//    }
//student-e-attendance
    public function zhuce()
    {
        $nickname = input("post.nickname");  //用户昵称
        $avatarurl = input("post.avatarurl");  //用户头像
        $role = input("role");
        $name = input("name");
        $phonenumber = input("phonenumber");
        $code = input("post.code");

        $arr = json_decode(openid($code), true);   //返回数组
        if (array_key_exists("openid", $arr)) {
            $openid = $arr['openid'];
//            return $role;
            $rule=[
                "name"=>"require|chs",
                "phonenumber"=>"require|mobile"
            ];
            $data = [
                'nickname' => $nickname,
                'avatarurl' => $avatarurl,
                'openid' => $openid,
                'role' => $role,
                'name' => $name,
                'phonenumber' => $phonenumber,
            ];
            try{
                $this->validate($data,$rule);
            }catch(\Exception $ex){
                return json(["error"=>true,"message"=>$ex->getMessage()]);
            }

            if ($role == "1") {
                $yh = Db::name("user")
                    ->where("openid", $openid)
                    ->where("phonenumber",$phonenumber)
                    ->find();
                if (empty($yh)) {
                    $count = Db::name("user")->insert($data);
                    $brr=register($count);
                    return $brr;
                } else {
                    return json(["error" => true, "message" => "账号已注册"]);
                }
            } else if ($role == "2") {
                $yh = Db::name("user")
                    ->where("openid", $openid)
                    ->where("phonenumber",$phonenumber)
                    ->find();
                if (empty($yh)) {
                    $count = Db::name("user")->insert($data);
                    $brr=register($count);
                    return $brr;
                } else {
                    return json(["error" => true, "message" => "账号已注册"]);
                }
            } else {
                return error("用户类型不正确");
            }
        } else {
            return json(['flag' => false, "msg" => "获取openid失败"]);
        }

    }

    public function login()
    {
        $code = input("post.code");
        if (empty($code)) {
            return ("code为空值");
        }

        $arr = json_decode(openid($code), true);
        if (array_key_exists("openid", $arr)) {
            $openid = $arr['openid'];
//            return $openid;
            $yh = Db::name("user")
                ->where("openid", $openid)
                ->select();
//               ->find();
//            return $yh;
            if (empty($yh)) {
                return json(["error" => false, "message" => "登录失败"]);
            } else {
                $user=[
                    "id"=>$yh[0],
                    "name"=>"zhangsan"
                ];
                session("user",$user);
                $token=Session::getId();
                return success(['token'=>$token]);
            }

        } else {
            return json(['flag' => false, "msg" => '获取openid失败']);
        }
    }

}
